<template>
  <div>
    <el-card style="margin:20px 0px;">
      <CategorySelect @getCategoryId="getCategoryId" :show="show"></CategorySelect>
    </el-card>
    <el-card>
      <div v-show="scene == 0">
        <el-button type="primary" icon="el-icon-plus" :disabled="!category3Id" @click="addSpu">添加SPU</el-button>
        <!-- 表格展示 -->
        <el-table style="width: 100%" border :data="spuList">
          <el-table-column type="index" label="序号" width="80" align="center">
          </el-table-column>
          <el-table-column prop="spuName" label="spu名称" width="150" align="center">
          </el-table-column>
          <el-table-column prop="description" label="spu描述" width="width" align="center">
          </el-table-column>
          <el-table-column prop="prop" label="操作" width="250">
            <template slot-scope="{row,$index}">
              <el-button type="success" icon="el-icon-plus" size="mini" title="添加sku" @click="addSku(row)"></el-button>
              <el-button type="warning" icon="el-icon-edit" size="mini" title="修改spu"
                @click="updateSpu(row)"></el-button>
              <el-button type="info" icon="el-icon-info" size="mini" title="查看当前spu的全部实例sku"
                @click="showSku(row)"></el-button>
              <el-popconfirm title="这是一段内容确定删除吗？" @onConfirm="deleteSpu(row)">
                <el-button type="danger" icon="el-icon-delete" size="mini" title="删除spu" slot="reference"></el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页器 -->
        <el-pagination :current-page="page" :total="total" :page-size="limit" :page-sizes="[3, 5, 10]" :pager-count="5"
          layout="prev, pager, next, jumper,->,sizes,total" style="margin-top:20px;text-align:center"
          @current-change="handleCurrentChange" @size-change="handleSizeChange">
        </el-pagination>
      </div>
      <!-- spu管理属性 -->
      <SpuForm v-show="scene == 1" @changeScene="changeScene" ref="spu"></SpuForm>
      <SkuForm v-show="scene == 2" @changeScene="changeScene" ref="sku"></SkuForm>
      <!-- 对话框 -->
      <el-dialog :title="`${spu.spuName}的sku列表`" :visible.sync="dialogVisible" width="30%" :before-close="Close">
        <!-- 表格展示sku -->
        <el-table :data="skuList" style="width: 100%" v-loading="loading">
          <el-table-column prop="skuName" label="名称" width="width">
          </el-table-column>
          <el-table-column prop="price" label="价格" width="width">
          </el-table-column>
          <el-table-column prop="weight" label="重量" width="width">
          </el-table-column>
          <el-table-column prop="prop" label="默认图片" width="width">
            <template slot-scope="{row,$index}">
              <img :src="row.skuDefaultImg" style="width:100px;height:100px">
            </template>
          </el-table-column>
        </el-table>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
// 引入子组件
import SpuForm from '@/views/product/spu/SpuForm'
import SkuForm from '@/views/product/spu/SkuForm'

export default {
  name: 'spu',
  data() {
    return {
      // 获取子组件传递的id值
      category1Id: '',
      category2Id: '',
      category3Id: '',

      // 控制显示和隐藏
      scene: 0,

      // 分页器相关
      page: 1,
      total: 1,
      limit: 3,

      // 获取请求获得的spu列表
      spuList: [],

      // 控制对话框显示和隐藏
      dialogVisible: false,
      // 接收查看sku列表得到的spu信息
      spu: {},
      // 接收当前spu下的sku列表信息
      skuList: [],

      // loading效果
      loading: true,
    }
  },
  components: {
    SpuForm,
    SkuForm
  },
  methods: {
    // 获取子组件传递的id值
    getCategoryId({ categoryId, level }) {
      if (level == 1) {
        this.category1Id = categoryId
      } else if (level == 2) {
        this.category2Id = categoryId
      } else {
        this.category3Id = categoryId
        this.getSpuList()
      }
    },
    // 获取spu列表
    async getSpuList() {
      let { page, limit, category3Id } = this
      let result = await this.$API.spu.reqGetSpuList(page, limit, category3Id)
      if (result.code == 200) {
        this.total = result.data.total
        this.spuList = result.data.records
      }
    },
    // 获取当前页数
    handleCurrentChange(page) {
      // 会将当前选中的页码传入
      this.page = page
      this.getSpuList()
    },
    // 修改每页展示条数时触发
    handleSizeChange(limit) {
      this.limit = limit
      this.getSpuList()
    },
    // 添加Spu按钮的回调
    addSpu() {
      this.scene = 1
      this.$refs.spu.AddSpuData(this.category3Id)
    },
    // 修改Spu按钮的回调
    updateSpu(row) {
      this.scene = 1,
        this.$refs.spu.UpdateSpuData(row)
    },
    // 删除Spu按钮的回调
    async deleteSpu(row) {
      let result = await this.$API.spu.reqDeleteSpu(row.id)
      if (result.code == 200) {
        this.$message({ type: 'success', message: '删除成功' })
        // 重新获取列表数据
        this.getSpuList()
      }
    },
    // 触发自定义事件
    changeScene({ scene, flag }) {
      this.scene = scene

      // 如果是添加spu要返回首页
      if (flag == '添加') {
        this.page = 1
        this.getSpuList()
      } else {
        this.getSpuList()
      }
    },
    // 添加sku按钮的回调(先显示spuForm信息)
    addSku(row) {
      this.scene = 2
      this.$refs.sku.getSkuForm(this.category1Id, this.category2Id, row)
    },
    // 查看当前spu的全部sku列表的回调
    async showSku(spu) {
      this.dialogVisible = true
      this.spu = spu
      let result = await this.$API.spu.reqSkuList(spu.id)
      if (result.code == 200) {
        this.skuList = result.data
        // 取消loading
        this.loading = false
      }
    },
    // 关闭对话框
    Close(done) {
      this.loading = true
      this.skuList = []
      // 关闭对话框
      done()
    },
  },
  computed: {
    // 根据数字转为布尔值
    show() {
      if (this.scene == 1 || this.scene == 2) {
        return true
      } else {
        return false
      }
    }
  },
}
</script>

<style></style>